﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOCUR.Models;

namespace SIOCUR.Controllers
{
    public class EquipoDinController : Controller
    {
        SIOCURRepository dbpRepository = new SIOCURRepository();
        //
        // GET: /EquipoDin/

        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult Registra()
        {
            return View("Registra");
        }

        [HttpPost]
        public ActionResult Registra(FormCollection formValues, EQUIPO_DINAMICO eqDin)
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            if(ModelState.IsValid)
            {
                dbpRepository.AddEqDinamico(estacion, sistema, DateTime.Now, Request.Form[0], Request.Form[1], Request.Form[2], Request.Form[3], Request.Form[4], Request.Form[5], Request.Form[6]);
                var equDin = dbpRepository.FindAllEqDinam().ToList();
                return View("Agregado", equDin);
            }
            else
            {
                return View();
            }
                //return View();

        }

        public ActionResult Todos()
        {
            var equDin = dbpRepository.FindAllEqDinam().ToList();
            return View(equDin);
        }

        public ActionResult Modifica()
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            return View(dbpRepository.GetEqDinams(estacion, sistema));
        }
        
        public ActionResult Modificar(int id)
        {
            EQUIPO_DINAMICO ed = dbpRepository.GetEqDinam(id);
            return View(ed);

        }

        [HttpPost]
        public ActionResult Modificar(int id, FormCollection formValues)
        {
            EQUIPO_DINAMICO ed = dbpRepository.GetEqDinam(id);
            if (TryUpdateModel(ed))
            {
                dbpRepository.Save();
                return RedirectToAction("Todos");
            }
            return View("Todos");
        }

    }
}
